# -*- coding: utf-8 -*-
"""
领域数据模型

@author: AI Assistant
"""
from typing import Optional, Dict, Any, List
from datetime import datetime
from pydantic import BaseModel, Field


class HydroModelSession(BaseModel):
    """水文模型会话数据"""
    session_id: str = Field(..., description="会话ID")
    stcd: str = Field(..., description="站点代码")
    stcd_name: Optional[str] = Field(None, description="站点名称")
    model_type: str = Field(..., description="模型类型（dhf, xaj 等）")
    plcd: Optional[str] = Field(None, description="方案代码（存储 model_id）")
    initial_state: Dict[str, Any] = Field(default_factory=dict, description="初始状态")
    parameters: Dict[str, Any] = Field(default_factory=dict, description="模型参数")
    para_schema: Dict[str, Any] = Field(default_factory=dict, description="参数模式定义")
    createdate: Optional[datetime] = Field(None, description="创建时间")
    md5: Optional[str] = Field(None, description="MD5 校验值")
    
    class Config:
        json_schema_extra = {
            "example": {
                "session_id": "abc123-def456-ghi789",
                "stcd": "21401550",
                "stcd_name": "大伙房水库",
                "model_type": "dhf",
                "plcd": "01324ac0-34cd-46f5-988f-c9b265b9c069",
                "initial_state": {
                    "water_level": 100.0,
                    "soil_moisture": 0.5
                },
                "parameters": {
                    "K": 0.5,
                    "X": 0.3
                },
                "para_schema": {
                    "initial_state": {
                        "water_level": {
                            "type": "float",
                            "min": 0.0,
                            "max": 200.0,
                            "unit": "m",
                            "description": "初始水位"
                        }
                    },
                    "parameters": {
                        "K": {
                            "type": "float",
                            "min": 0.0,
                            "max": 1.0,
                            "unit": "",
                            "description": "流量系数"
                        }
                    }
                }
            }
        }


class ConversationHistory(BaseModel):
    """对话历史记录"""
    conversation_id: str = Field(..., description="对话ID")
    session_id: str = Field(..., description="会话ID")
    timestamp: datetime = Field(default_factory=datetime.now, description="时间戳")
    role: str = Field(..., description="角色（user/assistant）")
    message: str = Field(..., description="消息内容")
    parameter_changes: Optional[Dict[str, Any]] = Field(None, description="参数修改（仅 assistant）")
    
    class Config:
        json_schema_extra = {
            "example": {
                "conversation_id": "conv_20240101_001",
                "session_id": "abc123-def456-ghi789",
                "timestamp": "2024-01-01T12:00:00",
                "role": "user",
                "message": "请将初始水位提高 10cm",
                "parameter_changes": None
            }
        }


class ParameterChange(BaseModel):
    """参数修改记录"""
    session_id: str = Field(..., description="会话ID")
    conversation_id: str = Field(..., description="对话ID")
    timestamp: datetime = Field(default_factory=datetime.now, description="修改时间")
    parameter_path: str = Field(..., description="参数路径（如 initial_state.water_level）")
    old_value: Any = Field(..., description="旧值")
    new_value: Any = Field(..., description="新值")
    reason: Optional[str] = Field(None, description="修改原因")
    
    class Config:
        json_schema_extra = {
            "example": {
                "session_id": "abc123-def456-ghi789",
                "conversation_id": "conv_20240101_001",
                "timestamp": "2024-01-01T12:00:05",
                "parameter_path": "initial_state.water_level",
                "old_value": 100.0,
                "new_value": 100.10,
                "reason": "用户要求提高 10cm"
            }
        }


class ParameterSchema(BaseModel):
    """参数定义模式"""
    type: str = Field(..., description="数据类型（float, int, str, bool）")
    min: Optional[float] = Field(None, description="最小值")
    max: Optional[float] = Field(None, description="最大值")
    unit: Optional[str] = Field(None, description="单位")
    description: Optional[str] = Field(None, description="参数说明")
    default: Optional[Any] = Field(None, description="默认值")
    
    class Config:
        json_schema_extra = {
            "example": {
                "type": "float",
                "min": 0.0,
                "max": 200.0,
                "unit": "m",
                "description": "初始水位（米）",
                "default": 100.0
            }
        }


class LLMResponse(BaseModel):
    """LLM 原始响应（用于解析）"""
    understood: bool = Field(..., description="是否理解用户意图")
    message: str = Field(..., description="回复消息")
    parameter_changes: Optional[Dict[str, Any]] = Field(None, description="参数修改建议")
    explanation: Optional[str] = Field(None, description="参数修改说明")
    
    class Config:
        json_schema_extra = {
            "example": {
                "understood": True,
                "message": "好的，我将初始水位提高 10cm",
                "parameter_changes": {
                    "initial_state.water_level": 100.10
                },
                "explanation": "将初始水位从 100.00 m 调整为 100.10 m"
            }
        }

